/**
 * Module dependencies.
 */

var util = require('util');
var express = require('express');
var http = require('http');
var path = require('path');
var routes = require('./routes');
var clusterUtils = require('./utils/cluster-utils');
var dataSource = require('./config/data-source-config');
var connectRateLimit = require('connect-ratelimit');
var app = express();

//config
var dbConfig = require('./config/data-source-config');
var runtimeConfig = require('./config/system-config');

// all environments
app.set('port', process.env.PORT || runtimeConfig.connection.http.port);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon(__dirname + '/public/images/logo/ani_small.png'));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);

//connect rate
app.use(connectRateLimit());

//routes
routes.setAppRoutes(app);

//db
dataSource.dbUtil.connect();

app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
    app.use(express.errorHandler());
}

http.createServer(app).listen(app.get('port'), function(){
    console.log('Anicel Device Manager server is listening on port ' + app.get('port'));
});
clusterUtils.createClusterServer(app, runtimeConfig.cluster.threadNumber);
